package org.apache.oozie.tools.workflowgenerator.client.property.action;

import java.util.ArrayList;

import org.apache.oozie.tools.workflowgenerator.client.property.PropertyTable;
import org.apache.oozie.tools.workflowgenerator.client.widget.NodeWidget;

import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.xml.client.Document;
import com.google.gwt.xml.client.Element;

public class EmailPropertyTable extends PropertyTable {

    protected Grid grid;
    protected TextBox to;
    protected TextBox cc;
    protected TextBox subject;
    protected TextBox body;

    public EmailPropertyTable(NodeWidget w) {
        super(w);
        initConf();
        initWidget();
    }

    /**
     * Initialize configuration
     */
    protected void initConf() {

        widgetList = new ArrayList<NodeWidget>();
        updateWidgetList();
        widgetDropDown = new ArrayList<NodeWidget>();
        updateWidgetDropDown();
    }

    /**
     * Generate xml elements of email action and attach them to xml doc
     */
    public void generateXML(Document doc, Element root, NodeWidget next) {

        Element action = doc.createElement("action");
        action.setAttribute("name", current.getName());

        // create <email>
        Element emailele = doc.createElement("email");
        action.appendChild(emailele);

        // create <to>
        Element toele = doc.createElement("to");
        toele.appendChild(doc.createTextNode(to.getText()));
        emailele.appendChild(toele);

        // create <cc>
        Element ccele = doc.createElement("cc");
        ccele.appendChild(doc.createTextNode(cc.getText()));
        emailele.appendChild(ccele);

        // create <subject>
        Element subjectele = doc.createElement("subject");
        subjectele.appendChild(doc.createTextNode(subject.getText()));
        emailele.appendChild(subjectele);

        // create <body>
        Element bodyele = doc.createElement("body");
        bodyele.appendChild(doc.createTextNode(body.getText()));
        emailele.appendChild(bodyele);

        // create <ok>
        Element okele = doc.createElement("ok");
        okele.setAttribute("to", next.getName());
        action.appendChild(okele);

        // create <error>
        Element errele = doc.createElement("error");
        NodeWidget kill = getKillNode();
        errele.setAttribute("to", kill == null ? "" : kill.getName());
        action.appendChild(errele);

        root.appendChild(action);

    }

    /**
     * Initialize widgets shown in property table
     */
    protected void initWidget() {

        grid = new Grid(7, 2);
        this.add(grid);

        this.setAlwaysShowScrollBars(true);
        this.setSize("100%", "80%");

        // insert row for Name
        grid.setWidget(0, 0, createLabel("Name"));
        name = new TextBox();
        name.setText("Email"); // default value
        grid.setWidget(0, 1, formatCell(name));

        // insert row for OK
        grid.setWidget(1, 0, createLabel("OK"));
        okVal = new ListBox();
        createWidgetDropDownList(okVal);
        grid.setWidget(1, 1, formatCell(okVal));

        // insert row for ERROR
        grid.setWidget(2, 0, createLabel("Error"));
        errorVal = new ListBox();
        updateErrorDropDownList();
        grid.setWidget(2, 1, formatCell(errorVal));

        // insert row for TO
        grid.setWidget(3, 0, createLabel("To"));
        to = new TextBox();
        grid.setWidget(3, 1, formatCell(to));

        // insert row for CC
        grid.setWidget(4, 0, createLabel("CC"));
        cc = new TextBox();
        grid.setWidget(4, 1, formatCell(cc));

        // insert row for Subject
        grid.setWidget(5, 0, createLabel("Subject"));
        subject = new TextBox();
        grid.setWidget(5, 1, formatCell(subject));

        // insert row for Body
        grid.setWidget(6, 0, createLabel("Body"));
        body = new TextBox();
        grid.setWidget(6, 1, formatCell(body));
    }
}
